﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.WeixinMP.Impls;
using Microhard.WeixinMP.Interfaces;
using Microhard.WeixinMP.Models;
using Microhard.Permission.Interface;
using Microhard.Core;
using Micrhard.FMCG.Server.Msg;
using Microhard.WeixinMP.Unities;
using Micrhard.FMCG.Server.Interface;
using Microhard.Core.Net;

namespace Micrhard.FMCG.Server.Weixin
{
    public class GetKADisplayPic_TakeImage_3 : BaseResponseImpl
    {
        public override Microhard.WeixinMP.Interfaces.WeixinCommand Command
        {
            get
            {
                WeixinCommand wc = new WeixinCommand();
                wc.Command = "image";
                return wc;
            }
        }

        protected override Microhard.WeixinMP.Models.WeixinBaseData<object> CreateFeatureResponse(Microhard.WeixinMP.Models.RequestServiceModel request, Microhard.WeixinMP.Models.WeixinBaseData<object> response)
        {
            response.MsgType = WeixinMsgType.Text;
            response.FuncFlag = WeixinMsgType.FuncFlag_False;
            WeixinTxt result = new WeixinTxt();

            //验证用户是否合法
            IWeiXinAccount iwa = IoC.Resolve<IWeiXinAccount>();
            string realUserName = string.Empty;
            bool illegal = iwa.ValidateByWeiXinUserName(response.ToUserName, out realUserName);
            if (illegal)
            {
                //获得用户命令
                WeixinUserCommandQueue queue = new WeixinUserCommandQueue();
                var userCommand = queue.GetFromCache(response.ToUserName);
                if (userCommand == null)
                {
                    result.Content = KADisplayMsg.CanNotTakePic;
                }
                else
                {
                    //将陈列图片保存
                    IProductDisplayImage idp = IoC.Resolve<IProductDisplayImage>();
                    var image = request.Image;
                    string url = image.Content.PicUrl;
                    byte[] byteImage = url.ReadImageUrl();
                    var addResult = idp.Add(new Models.ProductDisplayImageModel
                    {
                        Conetnt = byteImage,
                        ImageID = Guid.NewGuid(),
                        SubKACode = userCommand.BusinessValue
                    });
                    if (addResult.IsSucceed)
                    {
                        result.Content = string.Format(ProductDisplayImageMsg.AddSucceed, userCommand.BusinessValue);
                    }
                    else
                    {
                        result.Content = string.Format(ProductDisplayImageMsg.AddRecFailed, userCommand.BusinessValue);
                    }
                }
            }
            else
            {
                result.Content = KADisplayMsg.NoneUser;
            }
            response.Content = result;
            return response;
        }
    }
}
